import os
import shutil

folderA_path = "/home/champrin/Downloads/23年数据集/分区赛/rock/联调hagon (copy)"
folderB_path = "/home/champrin/Downloads/23年数据集/分区赛/rock/offical (copy)/val/label"

for fileA in os.listdir(folderA_path):
    fileA_path = os.path.join(folderA_path, fileA)

    if os.path.isfile(fileA_path):
        fileB_path = os.path.join(folderB_path, fileA)

        if os.path.exists(fileB_path) and os.path.isfile(fileB_path):
            # 获取文件类型
            fileA_type = os.path.splitext(fileA)[1]

            # 检查B中是否有同名同类型的文件
            if os.path.splitext(fileB_path)[1] == fileA_type:
                # 替换B中的文件为A中的文件
                # shutil.copy2(fileA_path, folderB_path)
                os.remove(fileA_path)
                print(f"Replaced {fileB_path} with {fileA_path} and deleted {fileA_path}")

print("Replacement process completed.")